<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="trainCode">
        <el-input
          v-model="queryParams.trainCode"
          placeholder="请选择车次"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="date">
        <el-date-picker clearable
          v-model="queryParams.date"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="start">
        <el-input
          v-model="queryParams.start"
          placeholder="请输入出发站"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="end">
        <el-input
          v-model="queryParams.end"
          placeholder="请输入到达站"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查找</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['business:daily-train-ticket:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['business:daily-train-ticket:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['business:daily-train-ticket:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['business:daily-train-ticket:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 使用折叠面板展示分组数据 -->
    <el-collapse v-loading="loading">
      <el-collapse-item v-for="(group, index) in groupedTickets" :key="index" :name="index">
        <template slot="title">
          <div class="train-group-title">
            <span class="train-code">车次: {{ group.trainCode }}</span>
            <span class="train-route">{{ group.mainRoute.start }} → {{ group.mainRoute.end }}</span>
            <span class="train-time">{{ group.mainRoute.startTime }} → {{ group.mainRoute.endTime }}</span>
          </div>
        </template>

        <!-- 嵌套表格显示具体车票信息 -->
        <el-table :data="group.tickets" :row-style="{ height: '60px' }" @selection-change="handleSelectionChange">
          <el-table-column show-overflow-tooltip  label="id" type="index" align="center" prop="id" />
          <el-table-column show-overflow-tooltip  label="日期" align="center" prop="date">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip  label="车次编号" align="center" prop="trainCode"/>
          <el-table-column show-overflow-tooltip  label="出发站/到达站" align="center" >
            <template slot-scope="scope">
              <span>{{ scope.row.start }} → {{ scope.row.end }}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip  label="出发/到站时间" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.startTime }} → {{ scope.row.endTime }}</span>
            </template>
          </el-table-column>
          <!-- 修改一等座列 -->
          <el-table-column show-overflow-tooltip  label="一等座" align="center">
            <template slot-scope="scope">
              <span v-if="Number(scope.row.ydz) <= 0" class="sold-out">售空</span>
              <span v-else>{{ scope.row.ydz }}张 → {{ scope.row.ydzPrice }}￥</span>
            </template>
          </el-table-column>

          <!-- 修改二等座列 -->
          <el-table-column show-overflow-tooltip  label="二等座" align="center" >
            <template slot-scope="scope">
              <span v-if="Number(scope.row.edz) <= 0" class="sold-out">售空</span>
              <span v-else>{{ scope.row.edz }}张 → {{ scope.row.edzPrice }}￥</span>
            </template>
          </el-table-column>

          <!-- 修改商务座列 -->
          <el-table-column show-overflow-tooltip  label="商务座" align="center" >
            <template slot-scope="scope">
              <span v-if="Number(scope.row.rw) <= 0" class="sold-out">售空</span>
              <span v-else>{{ scope.row.rw }}张 → {{ scope.row.rwPrice }}￥</span>
            </template>
          </el-table-column>

          <!-- 修改硬卧列 -->
<!--          <el-table-column show-overflow-tooltip  label="硬卧" align="center">-->
<!--            <template slot-scope="scope">-->
<!--              <span v-if="Number(scope.row.yw) <= 0" class="sold-out">售空</span>-->
<!--              <span v-else>{{ scope.row.yw }}张 → {{ scope.row.ywPrice }}￥</span>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column show-overflow-tooltip  label="出发站序" align="center" prop="startIndex" />
          <el-table-column show-overflow-tooltip  label="到站站序" align="center" prop="endIndex" />
        </el-table>
      </el-collapse-item>
    </el-collapse>

    <pagination
      v-show="trainTotal>0"
      :total="trainTotal"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改余票信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDailyTrainTicketByGroup, getDailyTrainTicket, delDailyTrainTicket, addDailyTrainTicket, updateDailyTrainTicket } from "@/api/business/daily-train-ticket";

export default {
  name: "Daily-train-ticket",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 总列车数
      trainTotal: 0,
      // 余票信息表格数据
      groupedTickets: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        date: null,
        trainCode: null,
        start: null,
        end: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        trainCode: [
          { required: true, message: "车次编号不能为空", trigger: "blur" }
        ],
        start: [
          { required: true, message: "出发站不能为空", trigger: "blur" }
        ],
        startPinyin: [
          { required: true, message: "出发站拼音不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "出发时间不能为空", trigger: "blur" }
        ],
        startIndex: [
          { required: true, message: "出发站序|本站是整个车次的第几站不能为空", trigger: "blur" }
        ],
        end: [
          { required: true, message: "到达站不能为空", trigger: "blur" }
        ],
        endPinyin: [
          { required: true, message: "到达站拼音不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "到站时间不能为空", trigger: "blur" }
        ],
        endIndex: [
          { required: true, message: "到站站序|本站是整个车次的第几站不能为空", trigger: "blur" }
        ],
        ydz: [
          { required: true, message: "一等座余票不能为空", trigger: "blur" }
        ],
        ydzPrice: [
          { required: true, message: "一等座票价不能为空", trigger: "blur" }
        ],
        edz: [
          { required: true, message: "二等座余票不能为空", trigger: "blur" }
        ],
        edzPrice: [
          { required: true, message: "二等座票价不能为空", trigger: "blur" }
        ],
        rw: [
          { required: true, message: "商务座余票不能为空", trigger: "blur" }
        ],
        rwPrice: [
          { required: true, message: "商务座票价不能为空", trigger: "blur" }
        ],
        // yw: [
        //   { required: true, message: "硬卧余票不能为空", trigger: "blur" }
        // ],
        // ywPrice: [
        //   { required: true, message: "硬卧票价不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询余票信息列表 */
    getList() {
      this.loading = true;
      listDailyTrainTicketByGroup(this.queryParams).then(response => {
        this.groupedTickets = response.rows;
        this.trainTotal = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        date: null,
        trainCode: null,
        start: null,
        startPinyin: null,
        startTime: null,
        startIndex: null,
        end: null,
        endPinyin: null,
        endTime: null,
        endIndex: null,
        ydz: null,
        ydzPrice: null,
        edz: null,
        edzPrice: null,
        rw: null,
        rwPrice: null,
        yw: null,
        ywPrice: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加余票信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDailyTrainTicket(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改余票信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDailyTrainTicket(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDailyTrainTicket(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除余票信息编号为"' + ids + '"的数据项？').then(function() {
        return delDailyTrainTicket(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/daily-train-ticket/export', {
        ...this.queryParams
      }, `daily-train-ticket_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.sold-out {
  color: red;
  font-weight: bold;
}

/* 添加折叠面板相关样式 */
.train-group-title {
  display: flex;
  align-items: center;
  font-size: 14px;
}

.train-code {
  font-weight: bold;
  margin-right: 20px;
  min-width: 80px;
}

.train-route {
  margin-right: 20px;
  min-width: 150px;
}

.train-time {
  color: #606266;
}

.el-collapse-item__header {
  height: 60px;
  line-height: 60px;
  background-color: #f5f7fa;
  padding-left: 15px;
}

.el-collapse-item__content {
  padding: 10px;
}
</style>
